package BiTree;

import java.util.*;

/**
 * @program: leetcode
 * @description: 二叉树层序遍历
 * @author: lxf
 * @create: 2024-06-20 22:50
 */
public class LevelOrder {
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new LinkedList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        list.offer(root);
        while (!list.isEmpty()) {
            List<Integer> levelList = new ArrayList<>();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = list.poll();
                levelList.add(node.val);
                if (node.left != null) {
                    list.offer(node.left);
                }
                if (node.right != null) {
                    list.offer(node.right);
                }
            }
            res.add(levelList);
        }
        return res;
    }

}
